from appium import webdriver


# 封装driver和元素操作
class BasePage:
    def __init__(self,driver):
        # 正气启动参数
        self.driver = driver
        self.driver.implicitly_wait(10)

    # 点击
    def click(self, selector):
        self.driver.find_element(*selector).click()

    # 输入
    def sendkeys(self, selector, content):
        self.driver.find_element(*selector).send_keys(content)

    # 滚动条
    def scrollTo(self):
        js = "window.scrollTo(0,1000);"
        self.driver.execute_script(js)

    # 滚动到指定元素
    def scrollTo_plus(self, selector):
        element = self.driver.find_element(*selector)  # 元素定位
        self.driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", element)

    # 切换句柄
    def switch_to(self):
        # 获取所有窗口的句柄
        handleList = self.driver.window_handles
        # 切换窗口  handleList[-1]表示最新的句柄也就是最新的窗口，handleList[0]指的是第一个窗口
        self.driver.switch_to_window(handleList[-1])

    # iframe标签切换
    def switch_to_iframe(self, selector):
        # 定位iframe标签
        iframe_element = self.driver.find_element(*selector)
        # 切换到表单
        self.driver.switch_to.frame(iframe_element)

    # 退出iframe
    def iframe_to_back(self):
        # 操作完之后  切换回最外层
        self.driver.switch_to.default_content()
